package servlet;

import com.alibaba.fastjson.JSON;
import dao.AllBuyedDao;
import dao.LeafletsDao;
import dao.PrizeDao;
import entiry.Prize;
import org.json.JSONObject;
import org.json.JSONString;
import util.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;

/**
 * 返回各个宣传页的图片 （不打开新的页面）
 */
public class LeafletImgServlet extends HttpServlet {

    DbUtil db = new DbUtil();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        String device_info = req.getParameter("device_info");
        String lotterValue = req.getParameter("lotterValue");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");  //这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();

//        System.out.println("device_info---" + device_info);
        LeafletsDao ld = new LeafletsDao(device_info);
        if (type != null) {
            try {
                Connection con = db.getCon();
                PrizeDao pd = new PrizeDao(device_info);
                switch (type) {
                    case "leaflet_1":
                        String leafletsOneContentImgUrl = ld.getLeafletsOneContentImgUrl(con);
                        if (null != leafletsOneContentImgUrl) {
                            writer.write(leafletsOneContentImgUrl);//宣传页
                        }
                        break;
                    case "leaflet_2":
                        //获取各个奖项的名字
                        ArrayList<Prize> allPrize = pd.getAllPrize(con);
                        Prize prize = allPrize.get(Integer.valueOf(lotterValue)-1);
//                        System.out.println("string---" + prize.getName());
                        String leafletsTwoContentImgUrl = ld.getLeafletsTwoContentImgUrl(con);
                        if (null != leafletsTwoContentImgUrl) {
                            JSONObject root = new JSONObject();
                            root.put("img_url", leafletsTwoContentImgUrl);
                            root.put("prize_name", prize.getName());
                            String string = root.toString();
                            writer.write(string);//领奖后的宣传页
                        }
                        break;
                    case "lingjiang":
                        //获取各个奖项的名字
                        ArrayList<Prize> allPrize1 = pd.getAllPrize(con);
                        Prize prize1 = allPrize1.get(Integer.valueOf(lotterValue)-1);
//                        System.out.println("string---" + prize.getName());
                        String leafletsReceivePrizePagerUrl = ld.getLeafletsReceivePrizePagerUrl(con);
//                        System.out.println("string---" + leafletsReceivePrizePagerUrl);
                        if (null != leafletsReceivePrizePagerUrl) {
                            JSONObject root = new JSONObject();
                            root.put("img_url", leafletsReceivePrizePagerUrl);
                            root.put("prize_name", prize1.getName());
                            String string = root.toString();
//                            System.out.println("string---" + string);
                            writer.write(string);//领奖页的图片
                        }
                        break;
                }
                DbUtil.getClose(con);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}